#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QDeclarativeItem>

#include "returnstatus.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    int i;
    QApplication app(argc, argv);
    const QStringList &args(QCoreApplication::arguments());
    ReturnStatus returnStatus;
    QDeclarativeView view;

    view.rootContext()->setContextObject(&returnStatus);

    if ((i = args.indexOf("--title")) != -1) {
        if (++i < args.size()){
            view.rootContext()->setContextProperty("titleFromQt", args.at(i));
        }
    } else {
        view.rootContext()->setContextProperty("titleFromQt", "QtDialog");
    }

    if ((i = args.indexOf("--yesno")) != -1) {
        if (++i < args.size()){
            view.rootContext()->setContextProperty("textFromQt", args.at(i));
        }
        view.rootContext()->setContextProperty("pageFromQt", "qrc:/qml/QtDialog/YesNo.qml");
    } else if ((i = args.indexOf("--msgbox")) != -1) {
        if (++i < args.size()){
            view.rootContext()->setContextProperty("textFromQt", args.at(i));
        }
        view.rootContext()->setContextProperty("pageFromQt", "qrc:/qml/QtDialog/MsgBox.qml");
    } else if ((i = args.indexOf("--inputbox")) != -1) {
        if (++i < args.size()){
            view.rootContext()->setContextProperty("textFromQt", args.at(i));
        }
        view.rootContext()->setContextProperty("pageFromQt", "qrc:/qml/QtDialog/InputBox.qml");
    } else if ((i = args.indexOf("--listbox")) != -1) {
        if (++i < args.size()){
            view.rootContext()->setContextProperty("textFromQt", args.at(i));
        }
        QTextStream qtin(stdin);
        QStringList dataList = qtin.readAll().split(QRegExp("\\n"), QString::SkipEmptyParts);

        view.rootContext()->setContextProperty("myModel", QVariant::fromValue(dataList));
        view.rootContext()->setContextProperty("pageFromQt", "qrc:/qml/QtDialog/ListBox.qml");
    } else {
        view.rootContext()->setContextProperty("pageFromQt", "qrc:/qml/QtDialog/MainPage.qml");
    }

    view.setSource(QUrl("qrc:/qml/QtDialog/main.qml"));

    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);

    QObject::connect((QObject*)view.engine(), SIGNAL(quit()), &app, SLOT(quit()));

#if defined(Q_WS_S60) || defined(Q_WS_MAEMO)
    view.showMaximized();
#else
    view.showFullScreen();
#endif

    return app.exec();
}
